﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using BlogEngine.Core.Providers;

namespace BlogEngine.Core
{
	/// <summary>
	/// 
	/// </summary>
	[Serializable, DataObject]
	public class Attachment : Postable<Attachment>
	{
		#region Constructor

		/// <summary>
		/// Initializes a new instance of the <see cref="Post"/> class.
		/// </summary>
		public Attachment()
		{
			DateCreated = DateTime.Now;

			BeginInit();
			Status = PostStatusType.Published;
			CommentStatus = PostCommentStatusType.Closed;
			EndInit();
		}

		#endregion

		/// <summary>
		/// Gets the attachments.
		/// </summary>
		/// <param name="startIndex">The start index.</param>
		/// <param name="count">The count.</param>
		/// <returns></returns>
		public static List<Attachment> GetAttachments(int startIndex, int count)
		{
			return BlogService.SelectAttachments(startIndex, count);
		}

		/// <summary>
		/// Gets the count.
		/// </summary>
		/// <value>The count.</value>
		public static long Count
		{
			get { return BlogService.CountAttachments(); }
		}

		#region Base overrides

		/// <summary>
		/// Validates the Post instance.
		/// </summary>
		protected override void Validate()
		{
			AddError(Parent == null, "Parent", "The parent must be set on an attachment.");
			AddError(String.IsNullOrEmpty(MimeType), "MimeType", "The MIME type must be set on an attachment.");
			AddError(String.IsNullOrEmpty(Title), "Title", "Title must be set.");
		}

		/// <summary>
		/// Returns a Post based on the specified id.
		/// </summary>
		protected override Attachment DataSelect(int id)
		{
			return BlogService.SelectAttachment(id);
		}

		/// <summary>
		/// Updates the Post.
		/// </summary>
		protected override void DataUpdate()
		{
			BlogService.UpdateAttachment(this);
		}

		/// <summary>
		/// Inserts a new post to the current BlogProvider.
		/// </summary>
		protected override void DataInsert()
		{
			BlogService.InsertAttachment(this);
		}

		/// <summary>
		/// Deletes the Post from the current BlogProvider.
		/// </summary>
		protected override void DataDelete()
		{
			BlogService.DeleteAttachment(this);
		}

		/// <summary>
		/// Gets if the Post have been changed.
		/// </summary>
		/// <value></value>
		/// <returns>true if the object’s content has changed since the last call to <see cref="M:System.ComponentModel.IChangeTracking.AcceptChanges"></see>; otherwise, false.</returns>
		public override bool IsChanged
		{
			get
			{
				if (base.IsChanged)
					return true;

				if (Terms.IsChanged)
					return true;

				return false;
			}
		}

		/// <summary>
		/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		/// </returns>
		public override string ToString()
		{
			return PermaLink.ToString();
		}

		#endregion
	}
}
